11088 - End up with more teams (DP), 11282 - Mixing invitations & 1481 - Arrange...
[and.git] / Mi manual de algoritmos / version_maraton_interuniversitaria_2008-2 / src / number_theory / bigmod.cpp
blob3d63b5548df3c7bc6639f9c77af7ed86b9b265c0
1 //retorna (b^p)mod(m)
2 // 0 <= b,p <= 2147483647
3 // 1 <= m <= 46340
4 long f(long b, long p, long m){
5 long mask = 1;
6 long pow2 = b % m;
7 long r = 1;
9 while (mask){
10 if (p & mask)
11 r = (r * pow2) % m;
12 pow2 = (pow2*pow2) % m;
13 mask <<= 1;
15 return r;